﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.Warehouse;
using Warehouse.Domain.Entities;
using Warehouse.Result;

namespace Warehouse.Api.Controllers
{
    /// <summary>
    /// 仓库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WareHouseController : ControllerBase
    {
        private readonly IWarehouseModelAppService _warehouseModelAppService;
        private readonly ILogger<WareHouseController> _logger;

        public WareHouseController(IWarehouseModelAppService warehouseModelAppService, ILogger<WareHouseController> logger)
        {
            _warehouseModelAppService = warehouseModelAppService;
            _logger = logger;
        }

        /// <summary>
        /// 创建仓库定义
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> CreateWareHouse(CreateWareHouseInput input)
        {
            _logger.LogInformation("添加了一条仓库定义：" + input.WareHouseName);
           return _warehouseModelAppService.CreateWareHouse(input);
        }


        /// <summary>
        /// 获取仓库定义列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        [HttpGet]
        public ApiPaing<ListWareHouseOutput> GetWareHouse([FromQuery]PageInput input)
        {
            return _warehouseModelAppService.GetWareHouse(input);
        }



        /// <summary>
        /// 逻辑删除仓库定义信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> DeleteWareHouse(long Id)
        {
            _logger.LogError("删除仓库定义信息:"+Id);
            return _warehouseModelAppService.DeleteWareHouse(Id);
        }


        /// <summary>
        /// 修改仓库定义
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateWareHouse(UpdateWareHouseInput input)
        {
            _logger.LogError("修改仓库定义:"+input.Id);
            return _warehouseModelAppService.UpdateWareHouse(input);
        }


        /// <summary>
        /// 获取仓库定义信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>`
        [HttpGet]
        public ApiResult<WareHouseModel> GetModelWareHouse(long Id)
        {
           return _warehouseModelAppService.GetModelWareHouse(Id);
        }


        /// <summary>
        /// 获取仓库类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<MoIdModel> GetMoid()
        {
            return _warehouseModelAppService.GetMoid();
        }

        /// <summary>
        /// 仓库定义导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Derive()
        {
            var book = new XSSFWorkbook();
            var a = book.CreateSheet("标题");
            var row = a.CreateRow(0);
            row.CreateCell(0).SetCellValue("仓库定义编号");
            row.CreateCell(1).SetCellValue("仓库定义编码");
            row.CreateCell(2).SetCellValue("仓库定义名称");
            row.CreateCell(3).SetCellValue("仓库定义类型");
            row.CreateCell(4).SetCellValue("仓管员");
            row.CreateCell(5).SetCellValue("地址");
            row.CreateCell(6).SetCellValue("描述");

            var index = 1;
            var bs = _warehouseModelAppService.GetWareHouse(new PageInput { PageIndex = 1, PageSize = 1000 });
            foreach (var s in bs.PageDatas)
            {
                var rows = a.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(s.Id);
                rows.CreateCell(1).SetCellValue(s.WareHouseCoding);
                rows.CreateCell(2).SetCellValue(s.WareHouseName);
                rows.CreateCell(3).SetCellValue(s.loginName);
                rows.CreateCell(4).SetCellValue(s.Loginid);
                rows.CreateCell(4).SetCellValue(s.WareHouseAddress);
                rows.CreateCell(4).SetCellValue(s.WareHouseDescribe);
            }

            byte[] sb = null;
            using (var ms = new MemoryStream())
            {
                book.Write(ms);
                sb = ms.ToArray();
            }
            return File(sb, System.Net.Mime.MediaTypeNames.Application.Octet, "库位设置打印.xlsx");

        }







    }
}
